package com.dingding.common.core.util.tinet;

import java.util.HashMap;
import java.util.Map;

import com.dingding.common.core.config.TinetConfig;
import com.dingding.common.core.util.MD5Util;

/**
 * @Description:天润融通电话号码保护http请求的参数生成器
 * @author fengshuonan
 * @date 2016年10月19日 上午11:15:45
 * @version 叮叮搭车
 * @since 2.4
 */
public class TinetRequest {

	private Map<String,String> map = new HashMap<String,String>();
	
	private long timestamp; 		//时间戳，linux时间戳
	private String sign;			//鉴权sign,md5(appId+token+timestamp),32位全小写
	private String method;			//请求的方法 set设置缓存  get读取缓存  delete删除缓存
	private String key;				//请求呼叫方的电话号码
	private String value;			//被呼叫方的电话号码（json形式）{tel:'18200000000'}
	private int expired;			//有效期秒数  默认3600秒，取值范围0-86400
	private int accessLimit;		//可供IVR平台访问次数，默认值10000
	
	public Map<String,String> getRequestMap(){
		this.map.put("appId", TinetConfig.APPID);
		this.map.put("timestamp", String.valueOf(timestamp));
		this.map.put("sign", sign);
		this.map.put("method", method);
		this.map.put("key", key);
		this.map.put("value", value);
		this.map.put("expired", String.valueOf(expired));
		this.map.put("accessLimit", String.valueOf(accessLimit));
		return this.map;
	}
	
	public static class Builder {
		private long timestamp = System.currentTimeMillis()/1000;
		private String sign = MD5Util.md5Encrypt(TinetConfig.APPID+TinetConfig.TOKEN+timestamp);
		private String method;
		private String key;
		private String value;
		private int expired = TinetConfig.EXPIRED;
		private int accessLimit = TinetConfig.ACCESSLIMIT;
        public Builder(){}
        public Builder timestamp(long timestamp) {
			this.timestamp = timestamp;
			return this;
		}
		public Builder sign(String sign) {
			this.sign = sign;
			return this;
		}
		public Builder method(TinetConfig.TinetMethod method) {
			this.method = method.toString();
			return this;
		}
		public Builder key(String key) {
			this.key = key;
			return this;
		}
		public Builder value(String value) {
			this.value = "{\"tel\":\"" + value + "\"}";
			return this;
		}
		public Builder expired(int expired) {
			this.expired = expired;
			return this;
		}
		public Builder accessLimit(int accessLimit) {
			this.accessLimit = accessLimit;
			return this;
		}
        public TinetRequest build() { return new TinetRequest(this);}
    }
	
	private TinetRequest(Builder builder){
		this.timestamp = builder.timestamp;
		this.sign = builder.sign;
		this.method = builder.method;
		this.key = builder.key;
		this.value = builder.value;
		this.expired = builder.expired;
		this.accessLimit = builder.accessLimit;
	}
	
	private TinetRequest(){
		
	}
}
